import 'dart:io';

import 'package:flutter/material.dart';
import 'package:xindong/common/xindong_sezhi.dart';
import 'package:xindong/common/xindong_define.dart';
import 'package:xindong/res/assets_res.dart';
import 'package:xindong/routes/user/auth/xindong_auth_ctr.dart';
import 'package:xindong/utils/xindong_image_util.dart';
import 'package:xindong/widgets/common/xindong_app_bar.dart';
import 'package:xindong/widgets/common/xindong_button.dart';

class XinDongAuthRealNameYeMian extends StatefulWidget {
  const XinDongAuthRealNameYeMian({super.key});

  @override
  State<XinDongAuthRealNameYeMian> createState() => _XinDongAuthRealNameYeMianState();
}

class _XinDongAuthRealNameYeMianState extends State<XinDongAuthRealNameYeMian> {
  File? faceFile;
  File? emblemFile;

  Future<void> _onTap(bool isFace) async {
    List<File>? list = await ImageUtil.selectPhotos(maxCount: 1);
    if (list != null) {
      setState(() {
        isFace ? faceFile = list.first : emblemFile = list.first;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: XinDongAppBar("实名认证"),
      body: ListView(
        padding: const EdgeInsets.all(kPaddingValue),
        children: [
          Padding(
            padding: const EdgeInsets.only(left: 32, right: 32, top: 10),
            child: AspectRatio(
              aspectRatio: 856 / 540.0,
              child: GestureDetector(
                onTap: () => _onTap(true),
                child: Container(
                  clipBehavior: Clip.antiAlias,
                  decoration: BoxDecoration(color: DZColors.cellColor, borderRadius: BorderRadius.circular(16)),
                  child: Stack(
                    fit: StackFit.expand,
                    children: [
                      Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          ImageUtil.asset(AssetsRes.IMAGE_AUTH_ADD, width: 64, height: 64),
                          const SizedBox(height: 10),
                          const Text(
                            "上传人像面照片",
                            style: TextStyle(fontSize: 16, color: DZColors.titleColor),
                          ),
                        ],
                      ),
                      // 照片
                      if (faceFile != null) Image.file(faceFile!, fit: BoxFit.contain)
                    ],
                  ),
                ),
              ),
            ),
          ),
          Padding(
            padding: const EdgeInsets.only(left: 32, right: 32, top: 20),
            child: AspectRatio(
              aspectRatio: 340 / 216.0,
              child: GestureDetector(
                onTap: () => _onTap(false),
                child: Container(
                  clipBehavior: Clip.antiAlias,
                  decoration: BoxDecoration(color: DZColors.cellColor, borderRadius: BorderRadius.circular(16)),
                  child: Stack(
                    fit: StackFit.expand,
                    children: [
                      Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          ImageUtil.asset(AssetsRes.IMAGE_AUTH_ADD, width: 64, height: 64),
                          const SizedBox(height: 10),
                          const Text(
                            "上传国徽面照片",
                            style: TextStyle(fontSize: 16, color: DZColors.titleColor),
                          ),
                        ],
                      ),
                      // 照片
                      if (emblemFile != null) Image.file(emblemFile!, fit: BoxFit.contain)
                    ],
                  ),
                ),
              ),
            ),
          ),
          Container(
            height: kButtonHeight,
            margin: const EdgeInsets.all(20),
            child: XinDongCommonButton('提交', onTap: () {
              List<File> files = [];
              if (faceFile != null && emblemFile != null) {
                files = [faceFile!, emblemFile!];
              }
              XinDongAuthCtr.to.requestAuthRealName(files: files);
            }),
          ),
          // <div class="tip-wrap">
          // <p class="title">{{_i18n('拍摄身份证要求')}}</p>
          // <p class="text">{{_i18n('大陆公民持有本人有效二代身份证')}}</p>
          // <p class="text">{{_i18n('拍摄时确保身份证')}}<span>{{_i18n('边框完整、字体清晰、亮度均匀')}}</span></p>
          // <img src="../../image/u20964.png" alt="">
          // </div>
          Container(
            alignment: Alignment.topLeft,
            padding: const EdgeInsets.all(20),
            margin: const EdgeInsets.only(bottom: 16.0),
            decoration: BoxDecoration(color: DZColors.cellColor, borderRadius: BorderRadius.circular(16)),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                const Text.rich(
                  TextSpan(
                    text: "拍摄身份证要求：\n",
                    style: TextStyle(color: DZColors.titleColor, fontSize: 16, fontWeight: FontWeight.w500),
                    children: [
                      TextSpan(
                        text: "大陆公民持有本人有效二代身份证\n",
                        style: TextStyle(color: DZColors.contentColor, fontSize: 14, height: 2.0),
                        children: [
                          TextSpan(text: "拍摄时确保身份证"),
                          TextSpan(text: "边框完整、字体清晰、亮度均匀", style: TextStyle(color: DZColors.mainColor)),
                        ],
                      ),
                    ],
                  ),
                ),
                const SizedBox(height: 8),
                AspectRatio(
                  aspectRatio: 541 / 152.0,
                  child: ImageUtil.asset(AssetsRes.IMAGE_AUTH_CARD, fit: BoxFit.cover),
                ),
              ],
            ),
          ),
          SizedBox(height: bottomBarHeight),
        ],
      ),
    );
  }
}
